package com.rjt.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**  
* @comment
* @author renjiangtao 
* @date 2018年9月6日  
* @version 1.0  
*/
public class FilesUtil {
	  public static String txt2String(File file){
	        StringBuilder result = new StringBuilder();
	        try{
	            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
	            String s = null;
	            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
	                result.append(System.lineSeparator()+s);
	            }
	            br.close();    
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        return result.toString();
	    }
	  public static void main(String[] args) throws UnsupportedEncodingException {
		    /*File file = new File("c:/lt.txt");
			 String str=FilesUtil.txt2String(file).trim();
			 byte[] bytes=Base64.getDecoder().decode(str);
			 str=new String(bytes,"utf-8");
			 String[] temp=str.split("\\|");
			 String temp1=temp[0];
			 System.out.println(temp1);*/
		  String2Txt("E:/rjt.txt","123123",false);
		  
	}
	public static void String2Txt(String file,String data,Boolean flag){
			try{
	            FileWriter fw = new FileWriter(file,flag);
	            BufferedWriter bf=new BufferedWriter(fw);
	            bf.append(data+"\r\n");
	            bf.close();
	            fw.close();
	           }catch(IOException e){
	            e.printStackTrace();
	           }
	    }
	public static void String2Txt(String file,String data){
		try{
            FileWriter fw = new FileWriter(file,true);
            BufferedWriter bf=new BufferedWriter(fw);
            bf.append(data+"\r\n");
            bf.close();
            fw.close();
           }catch(IOException e){
            e.printStackTrace();
           }
    }
}
